package com.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class SendMailService {

    @Value("${spring.mail.username}")  //发送的帐号
    private String sendUsername;

    @Autowired
    private JavaMailSender javaMailSender; //发邮邮件封装类(springboot)

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate rt;

    @Scheduled(cron = "0/5 * * * * ?")
    public void yzmExpire() {
        //删除要过期的数据 现在时间-分数>30*1000*60
        rt.boundZSetOps("yzm").removeRangeByScore(0, System.currentTimeMillis() - 30 * 1000 * 60);

    }


    public void zset排行() {
        //添加成绩
        rt.boundZSetOps("语文").add("张三", 80);
        rt.boundZSetOps("语文").add("李四", 90);
        rt.boundZSetOps("语文").add("王五", 70);
        rt.boundZSetOps("语文").add("赵六", 60);
        rt.boundZSetOps("语文").add("陈七", 75);
        //取前三名,降序 , reverseRangeWithScores可以返回value和score
        // reverseRange 只返回value
        Set<ZSetOperations.TypedTuple> set = rt.boundZSetOps("语文").reverseRangeWithScores(0, 2);
        for (ZSetOperations.TypedTuple t : set) {
            System.out.println(t.getValue() + "\t" + t.getScore());
        }

    }


    //求附近的人
    public void nearPersion() {
        //添加测试数据
        rt.boundGeoOps("p").add(new Point(118.803805,32.060168), "南京");
        rt.boundGeoOps("p").add(new Point(116.397039,39.9077), "北京");
        rt.boundGeoOps("p").add(new Point(113,28.21	), "长沙");
        rt.boundGeoOps("p").add(new Point(113.16,27.83	), "株洲");

        //计算两个间的位置
        Distance d = rt.boundGeoOps("p").distance("株洲", "北京", Metrics.KILOMETERS);
        System.out.println(d.getValue() + "km");

        //求株洲附近的人，查找范围为100公里，只返回最近三人
        RedisGeoCommands.GeoRadiusCommandArgs geoRadiusArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
        geoRadiusArgs.includeDistance().includeCoordinates().sortAscending().limit(3);

        GeoResults<RedisGeoCommands.GeoLocation> result = rt.boundGeoOps("p").radius("株洲", new Distance(100, Metrics.KILOMETERS),geoRadiusArgs);
        for (GeoResult<RedisGeoCommands.GeoLocation> r : result) {

            RedisGeoCommands.GeoLocation location = r.getContent(); //好友的位置
            Distance distance = r.getDistance(); //好友跟我的距离
            System.out.println(location.getName() + "\t" + distance.getValue()); //好友的名字

        }


    }


    public void sendMail(String to, String subject, String content) {
        //保存到redis中
        //rt.boundValueOps(to).set(content,30, TimeUnit.MINUTES); //使用string
        rt.boundZSetOps("yzm").add(to + ":" + content, System.currentTimeMillis());


        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);//收信人
        message.setSubject(subject);//主题
        message.setText(content);//内容
        message.setFrom(sendUsername);//发信人
        javaMailSender.send(message);
    }


    /**
     * 验证邮验证码
     *
     * @param toMail
     * @param code
     * @return
     */
    public String yz(String toMail, String code) {
        //String obj = (String)rt.boundValueOps(toMail).get(); //String

        //找集合中排名,如果-1则无次，其实就元素不存在集合中
        Long obj = rt.boundZSetOps("yzm").rank(toMail + ":" + code);
        if (obj == null)
            return "false";

        return "true";


    }
}
